package
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	
	public class VideoPolaroid extends Polaroid
	{
		
		private var streamingVideo:StreamingVideoPlayer;	//Neues 2D Video
		private var videoBMP:BitmapData;					//Bitmapdata - Soll Video auslesen
		private var fillPlane:Plane;						//Informationen zu einem Video
		private var videoMaterial:BitmapMaterial;			//Material für die videoPlane
		private var transMatrix:Matrix;
		
		public function VideoPolaroid()
		{
			init();
		}
		
		private function init():void{

			var vwidth:int = 640;
			var vheight:int = 368;
			
			//Neues Video
			//Die Videomasse kommen nachher aus der DB
			streamingVideo = new StreamingVideoPlayer(null,"beamer.flv",15,vwidth,vheight);
			//Bitmapdata mit der selben Auflösung wie das Video
			videoBMP = new BitmapData(1000,1000,false,0x131313);
			//Erstellen des Materials für den Plane - BitmapMaterial mit Video		
			videoMaterial = new BitmapMaterial(videoBMP);
			videoMaterial.interactive = true;
			videoMaterial.doubleSided = true;

			contentPlane.material = videoMaterial;
			contentPlane.y = -525;
			contentPlane.x = 0;
			contentPlane.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS,playVideo);
		
			//Transformation des Videos berechnen
			transMatrix = new Matrix;
			
			
			var vScale:Number = new Number();
			//Skalieren
			if(vwidth > vheight){
				vScale = 1000 / vwidth;
				transMatrix.scale(vScale,vScale);
				
				//verschiebung
				transMatrix.translate(0,(1000-vheight*vScale)/2);
			}
			else{
				vScale = 1000 / vheight;
				transMatrix.scale(vScale,vScale);
				
				//verschiebung
				transMatrix.translate((1000-vwidth*vScale)/2,0);
			}
			
			//Dieses Polaroid braucht ein OnEnterframe Update
			this.enableUpdate();
			
		}
		
		private function playVideo(e:InteractiveScene3DEvent):void{
			this.streamingVideo.playFlv();
			
		}
		
		override public function update():void{	
			videoBMP.draw(streamingVideo,transMatrix);
			videoMaterial.updateBitmap();	
		}

	}
}